using ClosedXML.Excel;
using VotingSystem.Models;

namespace VotingSystem.Services
{
    public class ExcelExportService
    {
        public byte[] ExportSurveyToExcel(Survey survey)
        {
            using (var workbook = new XLWorkbook())
            {
                // 创建问卷信息工作表
                var infoSheet = workbook.Worksheets.Add("问卷信息");
                infoSheet.Style.Font.FontName = "微软雅黑";
                infoSheet.Cell("A1").Value = "标题";
                infoSheet.Cell("B1").Value = survey.Title;
                infoSheet.Cell("A2").Value = "描述";
                infoSheet.Cell("B2").Value = survey.Description;
                infoSheet.Cell("A3").Value = "创建时间";
                infoSheet.Cell("B3").Value = survey.CreatedAt.ToString("yyyy-MM-dd HH:mm");
                infoSheet.Cell("A4").Value = "回答数";
                infoSheet.Cell("B4").Value = survey.Responses.Count;

                // 创建问题工作表
                var questionsSheet = workbook.Worksheets.Add("问题列表");
                questionsSheet.Style.Font.FontName = "微软雅黑";
                questionsSheet.Cell("A1").Value = "序号";
                questionsSheet.Cell("B1").Value = "问题";
                questionsSheet.Cell("C1").Value = "类型";
                questionsSheet.Cell("D1").Value = "选项";

                int row = 2;
                foreach (var question in survey.Questions.OrderBy(q => q.Order))
                {
                    questionsSheet.Cell($"A{row}").Value = question.Order;
                    questionsSheet.Cell($"B{row}").Value = $"{question.Order}. {question.QuestionText}";
                    questionsSheet.Cell($"C{row}").Value = question.Type.ToString();
                    questionsSheet.Cell($"D{row}").Value = string.Join(", ", question.Options.Select(o => o.OptionText));
                    row++;
                }

                // 创建回答工作表
                var responsesSheet = workbook.Worksheets.Add("回答列表");
                responsesSheet.Style.Font.FontName = "微软雅黑";
                responsesSheet.Cell("A1").Value = "回答者";
                responsesSheet.Cell("B1").Value = "提交时间";
                responsesSheet.Cell("C1").Value = "问题";
                responsesSheet.Cell("D1").Value = "回答";

                row = 2;
                foreach (var response in survey.Responses.OrderByDescending(r => r.SubmitTime))
                {
                    foreach (var questionResponse in response.QuestionResponses)
                    {
                        responsesSheet.Cell($"A{row}").Value = response.Respondent?.UserName ?? "匿名用户";
                        responsesSheet.Cell($"B{row}").Value = response.SubmitTime.ToString("yyyy-MM-dd HH:mm:ss");
                        responsesSheet.Cell($"C{row}").Value = $"{questionResponse.Question?.Order}. {questionResponse.Question?.QuestionText}";
                        string answer = "";
                        if (questionResponse.Question?.Type == QuestionType.Text)
                        {
                            answer = questionResponse.TextResponse ?? "";
                        }
                        else if (questionResponse.Question?.Type == QuestionType.Rating)
                        {
                            answer = questionResponse.RatingResponse.ToString();
                        }
                        else
                        {
                            answer = string.Join(", ", questionResponse.SelectedOptions.Select(o => o.Option?.OptionText));
                        }
                        responsesSheet.Cell($"D{row}").Value = answer;
                        row++;
                    }
                }

                // 设置样式
                foreach (var worksheet in workbook.Worksheets)
                {
                    worksheet.Style.Font.FontName = "微软雅黑";
                    var range = worksheet.Range(worksheet.FirstCell(), worksheet.LastCellUsed());
                    range.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    range.Style.Border.InsideBorder = XLBorderStyleValues.Thin;
                    worksheet.Columns().AdjustToContents();
                }

                using (var stream = new MemoryStream())
                {
                    workbook.SaveAs(stream);
                    return stream.ToArray();
                }
            }
        }

        public byte[] ExportVoteToExcel(Vote vote)
        {
            using (var workbook = new XLWorkbook())
            {
                // 创建投票信息工作表
                var infoSheet = workbook.Worksheets.Add("投票信息");
                infoSheet.Style.Font.FontName = "微软雅黑";
                infoSheet.Cell("A1").Value = "标题";
                infoSheet.Cell("B1").Value = vote.Title;
                infoSheet.Cell("A2").Value = "描述";
                infoSheet.Cell("B2").Value = vote.Description;
                infoSheet.Cell("A3").Value = "创建时间";
                infoSheet.Cell("B3").Value = vote.CreateTime.ToString("yyyy-MM-dd HH:mm");
                infoSheet.Cell("A4").Value = "截止时间";
                infoSheet.Cell("B4").Value = vote.EndTime.ToString("yyyy-MM-dd HH:mm");
                infoSheet.Cell("A5").Value = "总投票数";
                infoSheet.Cell("B5").Value = vote.Options.Sum(o => o.Votes);

                // 创建选项工作表
                var optionsSheet = workbook.Worksheets.Add("选项列表");
                optionsSheet.Style.Font.FontName = "微软雅黑";
                optionsSheet.Cell("A1").Value = "选项";
                optionsSheet.Cell("B1").Value = "描述";
                optionsSheet.Cell("C1").Value = "得票数";
                optionsSheet.Cell("D1").Value = "得票率";

                int row = 2;
                foreach (var option in vote.Options)
                {
                    optionsSheet.Cell($"A{row}").Value = option.Title;
                    optionsSheet.Cell($"B{row}").Value = option.Description;
                    optionsSheet.Cell($"C{row}").Value = option.Votes;
                    optionsSheet.Cell($"D{row}").Value = vote.TotalVotes == 0 ? 0 : (double)option.Votes / vote.TotalVotes;
                    row++;
                }

                // 创建投票记录工作表
                var recordsSheet = workbook.Worksheets.Add("投票记录");
                recordsSheet.Style.Font.FontName = "微软雅黑";
                recordsSheet.Cell("A1").Value = "投票者";
                recordsSheet.Cell("B1").Value = "投票时间";
                recordsSheet.Cell("C1").Value = "选项";
                recordsSheet.Cell("D1").Value = "评分";

                row = 2;
                foreach (var record in vote.Records.OrderByDescending(r => r.VoteTime))
                {
                    recordsSheet.Cell($"A{row}").Value = record.User?.UserName ?? "匿名用户";
                    recordsSheet.Cell($"B{row}").Value = record.VoteTime.ToString("yyyy-MM-dd HH:mm:ss");
                    recordsSheet.Cell($"C{row}").Value = record.Option?.Title;
                    recordsSheet.Cell($"D{row}").Value = record.Rating;
                    row++;
                }

                // 设置样式
                foreach (var worksheet in workbook.Worksheets)
                {
                    worksheet.Style.Font.FontName = "微软雅黑";
                    var range = worksheet.Range(worksheet.FirstCell(), worksheet.LastCellUsed());
                    range.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    range.Style.Border.InsideBorder = XLBorderStyleValues.Thin;
                    worksheet.Columns().AdjustToContents();
                }

                using (var stream = new MemoryStream())
                {
                    workbook.SaveAs(stream);
                    return stream.ToArray();
                }
            }
        }
    }
} 